<template>
  <div class="code-share-page">
    <!-- 大气的背景 -->
    <div class="grand-background">
      <div class="gradient-overlay"></div>
      <!-- <div class="subtle-pattern"></div> -->
    </div>

    <div class="ios-container">
      <div class="ios-navbar">
        <IOSBackButton @click="router.back()" />
      </div>
      
      <div class="header-section">
        <h1 class="page-title">代码分享平台</h1>
        <p class="page-subtitle">高效分享，优雅展示 · 让代码传递智慧</p>
      </div>

      <div class="main-content">
        <div class="code-share-container">
          <div class="input-section">
            <div class="input-row">
              <div class="language-selector">
                <label class="selector-label">选择编程语言</label>
                <select v-model="selectedLanguage" class="language-select">
                  <option value="javascript">JavaScript</option>
                  <option value="html">HTML</option>
                  <option value="css">CSS</option>
                  <option value="python">Python</option>
                  <option value="java">Java</option>
                  <option value="c">C</option>
                  <option value="cpp">C++</option>
                  <option value="php">PHP</option>
                  <option value="ruby">Ruby</option>
                  <option value="go">Go</option>
                  <option value="swift">Swift</option>
                  <option value="kotlin">Kotlin</option>
                  <option value="rust">Rust</option>
                  <option value="typescript">TypeScript</option>
                  <option value="shell">Shell</option>
                </select>
              </div>

              <div class="action-quick">
                <button class="quick-button primary" @click="shareCode">
                  <span class="button-icon">🚀</span>
                  立即分享
                </button>
                <button class="quick-button secondary" @click="copyCode">
                  <span class="button-icon">📋</span>
                  复制代码
                </button>
              </div>
            </div>

            <div class="code-input-wrapper">
              <label class="input-label">代码内容</label>
              <textarea
                v-model="inputCode"
                class="code-input"
                placeholder="在此输入您的代码片段，支持多种编程语言的语法高亮..."
                rows="12"
              />
            </div>
          </div>

          <div class="preview-section" v-if="inputCode">
            <div class="preview-header">
              <label class="preview-label">实时预览</label>
              <div class="preview-info">
                <span class="language-tag">{{ selectedLanguage.toUpperCase() }}</span>
                <span class="lines-count">{{ inputCode.split('\n').length }} 行</span>
              </div>
            </div>
            <div class="code-display">
              <pre><code class="hljs" :class="selectedLanguage" v-html="highlightedCode" /></pre>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from "vue";
import hljs from "highlight.js";
import "highlight.js/styles/github.css";
import IOSBackButton from "~/components/IOSBackButton.vue";

const router = useRouter();
const inputCode = ref("");
const selectedLanguage = ref("javascript");

const highlightedCode = computed(() => {
  if (!inputCode.value) return "";
  try {
    return hljs.highlight(inputCode.value, {
      language: selectedLanguage.value,
    }).value;
  } catch {
    return inputCode.value;
  }
});

function copyCode() {
  navigator.clipboard
    .writeText(inputCode.value)
    .then(() => alert("代码已复制到剪贴板"))
    .catch(() => alert("复制失败"));
}

async function shareCode() {
  if (!inputCode.value) {
    alert("请输入要分享的代码");
    return;
  }

  try {
    const { id } = await $fetch("/api/share-code", {
      method: "POST",
      body: { code: inputCode.value, lan: selectedLanguage.value },
    });

    const shareUrl = `${window.location.origin}/code-share/file/${id}`;
    navigator.clipboard
      .writeText(shareUrl)
      .then(() => alert(`分享链接已复制到剪贴板: ${shareUrl}`))
      .catch(() => alert("复制链接失败"));
  } catch (error) {
    alert("分享失败: " + error.message);
  }
}
</script>

<style scoped>
.code-share-page {
  min-height: 100vh;
  position: relative;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.grand-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #b68be1 100%);
  z-index: -2;
}

.gradient-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(45deg, 
    rgba(255, 255, 255, 0.1) 0%, 
    rgba(255, 255, 255, 0.05) 50%, 
    rgba(0, 0, 0, 0.05) 100%);
  z-index: -1;
}

.subtle-pattern {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: 
    radial-gradient(circle at 25% 25%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 75% 75%, rgba(255, 255, 255, 0.05) 0%, transparent 50%);
  background-size: 200px 200px;
  animation: patternMove 20s linear infinite;
}

@keyframes patternMove {
  0% { transform: translate(0, 0); }
  100% { transform: translate(100px, 100px); }
}

.ios-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px 40px;
  position: relative;
  z-index: 1;
}

.ios-navbar {
  padding: 20px 0;
  margin-bottom: 10px;
}

.header-section {
  text-align: center;
  margin-bottom: 30px;
}

.page-title {
  font-size: 3rem;
  font-weight: 800;
  color: white;
  margin-bottom: 16px;
  letter-spacing: -0.02em;
  text-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1rem;
  color: rgba(255, 255, 255, 0.9);
  margin: 0;
  font-weight: 400;
  letter-spacing: 0.5px;
}

.main-content {
  position: relative;
}

.code-share-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  padding: 30px;
  box-shadow: 
    0 20px 60px rgba(0, 0, 0, 0.1),
    0 8px 25px rgba(0, 0, 0, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.input-section {
  margin-bottom: 20px;
}

.input-row {
  display: flex;
  gap: 40px;
  margin-bottom: 10px;
  align-items: flex-end;
}

.language-selector {
  flex: 1;
}

.action-quick {
  display: flex;
  gap: 16px;
}

.selector-label,
.input-label,
.preview-label {
  display: block;
  font-size: 1.1rem;
  font-weight: 600;
  color: #1d1d1f;
  margin-bottom: 12px;
  letter-spacing: 0.3px;
}

.language-select {
  width: 100%;
  padding: 16px 20px;
  border-radius: 12px;
  border: 2px solid #e5e5ea;
  background-color: #f9f9f9;
  font-size: 1.1rem;
  color: #1d1d1f;
  transition: all 0.3s ease;
  appearance: none;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e");
  background-position: right 16px center;
  background-repeat: no-repeat;
  background-size: 18px;
  padding-right: 50px;
}

.language-select:focus {
  outline: none;
  border-color: #007aff;
  background-color: white;
  box-shadow: 0 0 0 4px rgba(0, 122, 255, 0.1);
  transform: translateY(-2px);
}

.code-input-wrapper {
  position: relative;
}

.code-input {
  width: 100%;
  padding: 24px;
  border-radius: 16px;
  border: 2px solid #e5e5ea;
  background-color: #f9f9f9;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
  font-size: 1rem;
  line-height: 1.7;
  resize: vertical;
  min-height: 250px;
  color: #1d1d1f;
  transition: all 0.3s ease;
}

.code-input:focus {
  outline: none;
  border-color: #007aff;
  background-color: white;
  box-shadow: 0 0 0 4px rgba(0, 122, 255, 0.1);
  transform: translateY(-2px);
}

.code-input::placeholder {
  color: #86868b;
  font-style: italic;
}

.preview-section {
  margin-bottom: 20px;
}

.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.preview-info {
  display: flex;
  gap: 16px;
  align-items: center;
}

.language-tag {
  background: #007aff;
  color: white;
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.lines-count {
  color: #86868b;
  font-size: 0.95rem;
  font-weight: 500;
}

.code-display {
  background: #f8f9fa;
  border: 2px solid #e9ecef;
  border-radius: 16px;
  overflow: hidden;
  position: relative;
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.06);
}

.code-display pre {
  margin: 0;
  padding: 30px;
  overflow-x: auto;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
  font-size: 0.95rem;
  line-height: 1.6;
}

.code-display code {
  background: none !important;
  padding: 0 !important;
}

.quick-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 16px 24px;
  border-radius: 12px;
  border: none;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 140px;
  justify-content: center;
  white-space: nowrap;
}

.quick-button.primary {
  background: linear-gradient(135deg, #007aff 0%, #0051d5 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(0, 122, 255, 0.3);
}

.quick-button.primary:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(0, 122, 255, 0.4);
}

.quick-button.primary:active {
  transform: translateY(-1px);
}

.quick-button.secondary {
  background: rgba(255, 255, 255, 0.9);
  color: #1d1d1f;
  border: 2px solid #e5e5ea;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.quick-button.secondary:hover {
  background: white;
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
}

.quick-button.secondary:active {
  transform: translateY(-1px);
}

.button-icon {
  font-size: 1.2rem;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .ios-container {
    max-width: 1000px;
    padding: 30px;
  }
  
  .code-share-container {
    padding: 40px;
  }
}

@media (max-width: 768px) {
  .ios-container {
    padding: 20px;
  }
  
  .code-share-container {
    padding: 30px;
  }
  
  .page-title {
    font-size: 2.5rem;
  }
  
  .input-row {
    flex-direction: column;
    gap: 20px;
    align-items: stretch;
  }
  
  .action-quick {
    flex-direction: column;
  }
  
  .quick-button {
    flex: none;
  }
}

@media (max-width: 480px) {
  .code-share-container {
    padding: 24px;
  }
  
  .page-title {
    font-size: 2rem;
  }
  
  .page-subtitle {
    font-size: 1.1rem;
  }
}
</style>

